package com.yangzb.spring.batch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.support.ReferenceJobFactory;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.SimpleJobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.launch.support.SimpleJobOperator;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class Runner implements ApplicationRunner {
    @Autowired
    private JobLauncher jobLauncher;
//    @Autowired
//    private Job singleStepJob;

    @Resource(name = "cStepJob")
    private Job cStepJob;
    @Autowired
    JobRegistry jobRegistry;
    @Autowired
    JobExplorer jobExplorer;
    @Autowired
    JobRepository jobRepository;
    @Autowired
    SimpleJobOperator jobOperator;


    @Override
    public void run(ApplicationArguments args) throws Exception {
//        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
//                .toJobParameters();
//        jobLauncher.run(singleStepJob, jobParameters);

        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                .toJobParameters();
        jobLauncher.run(cStepJob, jobParameters);
//        restartUncompletedJobs();
    }

    public void restartUncompletedJobs() {

        try {

            jobRegistry.register(new ReferenceJobFactory(cStepJob));
//            JobInstance jobInstance = jobExplorer.getJobInstance(4L);
//            StepExecution uppercaseStep = jobRepository.getLastStepExecution(jobInstance, "uppercaseStep");
//            runningJob.setStatus(BatchStatus.FAILED);
//                        runningJob.setEndTime(new Date());
//                        jobRepository.update(runningJob);
//            uppercaseStep.setStatus(BatchStatus.FAILED);
//            jobRepository.update(uppercaseStep);
//            jobOperator.stop(4L);
            jobOperator.restart(9L);

//
//            List<String> jobs = jobExplorer.getJobNames();
//            for (String job : jobs) {
//                Set<JobExecution> runningJobs = jobExplorer.findRunningJobExecutions(job);
//
//                for (JobExecution runningJob : runningJobs) {
//                    if(runningJob.getJobId().equals(1L)){
//                        runningJob.setStatus(BatchStatus.FAILED);
//                        runningJob.setEndTime(new Date());
//                        jobRepository.update(runningJob);
//                    }
//
//                }
//            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

